package com.kalvan.db.mybatis.provider;

import com.kalvan.db.mybatis.util.ShardingTableHelper;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.mapper.mapperhelper.MapperTemplate;
import tk.mybatis.mapper.mapperhelper.SqlHelper;
import tk.mybatis.mapper.util.MetaObjectUtil;

/**
 * 针对分表,id不可用使用uk 支持
 *
 * @author chenliang
 */
public class ShardingProvider extends MapperTemplate {

    public ShardingProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
        super(mapperClass, mapperHelper);
    }

    public String deleteByUk(MappedStatement ms) {
        Class<?> entityClass = this.getEntityClass(ms);
        StringBuilder sql = new StringBuilder();
        if (SqlHelper.hasLogicDeleteColumn(entityClass)) {
            sql.append(SqlHelper.updateTable(entityClass, this.tableName(entityClass)));
            sql.append("<set>");
            sql.append(SqlHelper.logicDeleteColumnEqualsValue(entityClass, true));
            sql.append("</set>");
            MetaObjectUtil.forObject(ms).setValue("sqlCommandType", SqlCommandType.UPDATE);
        } else {
            sql.append(SqlHelper.deleteFromTable(entityClass, this.tableName(entityClass)));
        }

        sql.append(ShardingTableHelper.whereShardingUkColumns(entityClass));
        return sql.toString();
    }


    public String selectByUk(MappedStatement ms) {
        Class<?> entityClass = this.getEntityClass(ms);
        this.setResultType(ms, entityClass);
        StringBuilder sql = new StringBuilder();
        sql.append(SqlHelper.selectAllColumns(entityClass));
        sql.append(SqlHelper.fromTable(entityClass, this.tableName(entityClass)));
        sql.append(ShardingTableHelper.whereShardingUkColumns(entityClass));
        return sql.toString();
    }


    public String existsWithUk(MappedStatement ms) {
        Class<?> entityClass = this.getEntityClass(ms);
        StringBuilder sql = new StringBuilder();
        sql.append(SqlHelper.selectCountExists(entityClass));
        sql.append(SqlHelper.fromTable(entityClass, this.tableName(entityClass)));
        sql.append(ShardingTableHelper.whereShardingUkColumns(entityClass));
        return sql.toString();
    }

//    public String updateByUk(MappedStatement ms) {
//        Class<?> entityClass = this.getEntityClass(ms);
//        StringBuilder sql = new StringBuilder();
//        sql.append(SqlHelper.updateTable(entityClass, this.tableName(entityClass)));
//        sql.append(SqlHelper.updateSetColumns(entityClass, (String) null, false, false));
//        sql.append(ShardingTableHelper.whereShardingUKColumns(entityClass));
//        return sql.toString();
//    }

    public String updateByUkSelective(MappedStatement ms) {
        Class<?> entityClass = this.getEntityClass(ms);
        StringBuilder sql = new StringBuilder();
        sql.append(SqlHelper.updateTable(entityClass, this.tableName(entityClass)));
        sql.append(SqlHelper.updateSetColumns(entityClass, null, true, this.isNotEmpty()));
        sql.append(ShardingTableHelper.whereShardingUkColumns(entityClass));
        return sql.toString();
    }
}
